//
//  IMIRNSourceCodeFileManager.h
//  IMIHome
//
//  Created by OrrHsiao on 2022/5/9.
//  Copyright © 2022 上海创米科技有限公司. All rights reserved.
//

#import <Foundation/Foundation.h>
@class IMIRNSourceCodeFileDownloadInfo;

#define DefaultsName_CustomRnVersion(model)               [NSString stringWithFormat:@"DefaultsName_RnVersion_%@", model]
#define DefaultsName_StandardRnVersion(model)               [NSString stringWithFormat:@"DefaultsName_StandardRnVersion_%@", model]
#define DefaultsName_CustomRnExtraInfo(model)               [NSString stringWithFormat:@"DefaultsName_CustomRnExtraInfo_%@", model]
#define DefaultsName_StandardRnExtraInfo(model)               [NSString stringWithFormat:@"DefaultsName_StandardRnExtraInfo_%@", model]

typedef void(^IMIRNSourceCodeFileDownloadCompletBlock)(NSString * _Nullable path, NSError * _Nullable error);
typedef void(^IMIRNSourceCodeFileDownloadProgressBlock)(double progress);
typedef void(^IMIRNSourceCodeFileQueryUpdateCompletBlock)(NSDictionary * _Nullable info, NSError * _Nullable error);

typedef NS_ENUM(NSInteger, IMIRNSourceCodeFileManagerErrorType) {
    /// 未知错误
    IMIRNSourceCodeFileManagerErrorTypeUnknown = 0,
    /// 解压失败
    IMIRNSourceCodeFileManagerErrorTypeZipError,
    /// MD5校验失败
    IMIRNSourceCodeFileManagerErrorTypeMD5Error,
    /// RN下载地址为空
    IMIRNSourceCodeFileManagerErrorTypeDownloadUrlError,
};

NS_ASSUME_NONNULL_BEGIN

#pragma mark - 下载信息实体类
@interface IMIRNSourceCodeFileDownloadInfo: NSObject

/// model
@property (nonatomic, copy) NSString *model;

/// tag
@property (nonatomic, copy) NSString *tag;

/// 下载地址
@property (nonatomic, copy) NSString *rnUrl;

/// 版本号
@property (nonatomic, copy) NSString *rnCode;

/// 是否是标准插件
@property (nonatomic, assign) BOOL isStandardPlugin;

/// 下载错误描述
@property (nonatomic, strong, nullable) NSError *error;

/// 下载进度
@property (nonatomic, assign) double progress;

@end

#pragma mark - 代理
@protocol IMIRNSourceCodeFileDownloadDelegate <NSObject>

@optional

/// 开始下载
/// - Parameter info: 下载信息
- (void)RNSourceCodeFileDownloadStart:(nonnull IMIRNSourceCodeFileDownloadInfo *)info;

/// 下载进度
/// - Parameter info: 下载信息
- (void)RNSourceCodeFileDownloadProgress:(nonnull IMIRNSourceCodeFileDownloadInfo *)info;

/// 下载完成
/// - Parameter info: 下载信息
- (void)RNSourceCodeFileDownloadComplet:(nonnull IMIRNSourceCodeFileDownloadInfo *)info;

@end

#pragma mark - 管理类
@interface IMIRNSourceCodeFileManager : NSObject

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)new NS_UNAVAILABLE;
- (id)copy NS_UNAVAILABLE;
- (id)mutableCopy NS_UNAVAILABLE;

/// 获取实例
+ (instancetype)sharedManager;

/// 添加代理
/// @param delegate <#delegate description#>
- (void)addDelegate:(nonnull id <IMIRNSourceCodeFileDownloadDelegate>)delegate;

/// 移除代理
/// @param delegate <#delegate description#>
- (void)removeDelegate:(nonnull id <IMIRNSourceCodeFileDownloadDelegate>)delegate;

/// 下载指定版本的RN插件
/// @param tag tag 唯一标识
/// @param model tag 唯一标识
/// @param did did
/// @param rnUrl 下载地址
/// @param rnCode 版本号
/// @param md5 md5
/// @param isStandardPlugin 是否是标准插件
/// @param extraInfo 额外信息
- (void)downloadRNSourceCodeWithTag:(nonnull NSString *)tag
                              model:(nonnull NSString *)model
                                   did:(nonnull NSString *)did
                           downloadUrl:(nonnull NSString *)rnUrl
                             rnVersion:(nonnull NSString *)rnCode
                                   md5:(nonnull NSString *)md5
                      isStandardPlugin:(BOOL)isStandardPlugin
                          extraInfo:(nullable NSString *)extraInfo;

/// 检查本地是否有已存在的自定义RN插件
/// @param model model
- (BOOL)checkHaveLocalCustomRNSourceCodeFileWithModel:(nonnull NSString *)model;

/// 检查本地是否有已存在的标插RN插件
/// @param pluginType 标插品类 ipcPlugin doorbellPlugin doorPlugin
/// @param version 标插版本 标插版本
- (BOOL)checkHaveLocalStandardRNSourceCodeFileWithPluginType:(nonnull NSString *)pluginType version:(nonnull NSString *)version;

/// 检查指定路径是否有已存在的RN插件
/// @param path <#path description#>
- (BOOL)checkHaveRNSourceCodeWithPath:(NSString *)path;

/// 获取自定义插件所在的文件夹地址
/// @param model model
- (nonnull NSString *)getCustomRNFileDirPathWithModel:(nonnull NSString *)model;

/// 获取标准插件所在文件夹地址
/// @param pluginType 标插品类 ipcPlugin doorbellPlugin doorPlugin
/// @param version 标插版本
- (nonnull NSString *)getStandardRNFileDirPathWithPluginType:(nonnull NSString *)pluginType version:(nonnull NSString *)version;

/// 删除本地自定义插件
/// @param model <#model description#>
- (void)deleteLocalCustomRNSourceCodeFileWithModel:(nonnull NSString *)model;

/// 获取指定设备当前使用的自定义插件的版本号
/// @param model model
- (nullable NSString *)getCustomLocalRNSourceCodeFileVersionWithModel:(nonnull NSString *)model;

/// 获取指定设备当前使用的标准插件的版本号,以pluginType_version作为版本号
/// @param model model
- (nullable NSString *)getStandardLocalRNSourceCodeFileVersionWithModel:(nonnull NSString *)model;

/// 获取自定义插件ExtraInfo
/// @param model <#model description#>
- (nullable NSString *)getCustomLocalRNExtraInfoWithModel:(nonnull NSString *)model;

/// 获取标准插件ExtraInfo
/// @param model <#model description#>
- (nullable NSString *)getStandardLocalRNExtraInfoWithModel:(nonnull NSString *)model;

/// 设置自定义插件ExtraInfo
/// @param model <#model description#>
/// @param info <#info description#>
- (void)setCustomLocalRNExtraInfoWithModel:(nonnull NSString *)model info:(nullable NSString *)info;

/// 设置标准插件ExtraInfo
/// @param model <#model description#>
/// @param info <#info description#>
- (void)setStandardLocalRNExtraInfoWithModel:(nonnull NSString *)model info:(nullable NSString *)info;

/// 获取文件的md5值
/// @param encryptFile <#encryptFile description#>
/// @param key <#key description#>
+ (NSString *)getMd5WithEncryptFile:(NSString *)encryptFile key:(NSString *)key;

@end

NS_ASSUME_NONNULL_END
